import backtrader as bt

class StrategyLookBar(bt.Strategy):
    params = (
        ("period", 5),  # 均线周期
    )

    def __init__(self):
        print(f'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!={self.datas[0]}')
        self.dataclose = self.datas[0].close
        self.sma = bt.indicators.SimpleMovingAverage(
            self.dataclose, period=self.params.period
        )

    def next(self):
        # 打印当前 bar 的详细信息
        print(f"Datetime: {self.data.datetime.datetime()}")
        print(f"Open: {self.data.open[0]}")
        print(f"High: {self.data.high[0]}")
        print(f"Low: {self.data.low[0]}")
        print(f"Close: {self.data.close[0]}")
        print(f"Volume: {self.data.volume[0]}")
        print(f"OpenInterest: {self.data.openinterest[0]}")
        print("----------------------------")
        print(f'= !!!!!!!!!!!!!!!!!!!!!!!!!!!ssss!!!={self.dataclose}')
        if self.dataclose > self.sma:
            self.buy()
            print('buy!!!!!!')
        elif self.dataclose < self.sma:
            self.sell()
            print('sell!!!!!!')